package com.common.project.exception;

import com.common.project.common.StatusCode;
import com.common.project.util.I18nUtil;
import com.common.project.util.Response;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.UnexpectedTypeException;
import java.util.Objects;

/**
 * @ClassName ExceptionHandler
 * @Description
 * @Author jiangshicun@bicai365.com
 * @Date 2019/6/20 13:58
 */
public class ExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(ExceptionHandler.class);

    public ExceptionHandler() {
    }

  /*  @org.springframework.web.bind.annotation.ExceptionHandler({FeignException.class, ClientException.class})
    public Response<?> feignException(Exception e) {
        log.error(e.getMessage(), e);
        return Response.failure(ErrorCode.S_MICRO_SERVICE_ERROR);
    }*/

    @org.springframework.web.bind.annotation.ExceptionHandler({BindException.class, UnexpectedTypeException.class})
    public Response<?> validateException(Exception e) {
        log.error("参数错误--==========" + e.getMessage(), e);
        return Response.failure(ErrorCode.P_COMMON_ERROR);
    }

    @org.springframework.web.bind.annotation.ExceptionHandler({BaseException.class})
    public Response<?> baseException(BaseException e) {
        log.error(e.getMessage(), e);
        return Response.failure(e.getErrCode());
    }

    @org.springframework.web.bind.annotation.ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public Response<?> handleException(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        BindingResult result = e.getBindingResult();
        final String message = ((FieldError) Objects.requireNonNull(result.getFieldError())).getDefaultMessage();
        return Response.failure(new StatusCode() {
            @Override
            public String getMessage() {
                return message;
            }
            @Override
            public String getCode() {
                String value = I18nUtil.getString(message);
                return StringUtils.isNotEmpty(message) && StringUtils.isNotEmpty(value) ? message : ErrorCode.P_COMMON_ERROR.getCode();
            }
        });
    }

    @org.springframework.web.bind.annotation.ExceptionHandler({IllegalArgumentException.class})
    public Response<?> groundException(final IllegalArgumentException e) {
        log.error(e.getMessage(), e);
        return Response.failure(new StatusCode() {
            @Override
            public String getMessage() {
                return e.getMessage();
            }
            @Override
            public String getCode() {
                return ErrorCode.S_SERVICE_UNAVAILABLE.getCode();
            }
        });
    }

    @org.springframework.web.bind.annotation.ExceptionHandler({Exception.class})
    public Response<?> exception(Exception e) {
        log.error("message:{} exception:{}", e.getMessage(), e);
        return Response.failure(ErrorCode.S_SERVICE_UNAVAILABLE);
    }
}
